Page({
  data: {
    tabIndex: 0,
    currentTab: 0,
    note: [
      {
        title: '去寻找的新的主人吧！',
        url: 'http://zq.jhcms.cn/attachs/photo/201711/20171130_176CFE51B6710715B1BBBEF2F86ACB0C.jpg',
      },
      {
        title: '去寻找的新的主人吧！',
        url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
      },
      {
        title: '去寻找的新的主人吧！',
        url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72',
      },
      {
        title: '去寻找的新的主人吧！',
        url: 'http://zq.jhcms.cn/attachs/photo/201711/20171130_9E39DA252E3946BE36218D85876C4AB4.jpg',
      },
      {
        title: '去寻找的新的主人吧！',
        url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg'
      },

      {
        title: '去寻找的新的主人吧！',
        url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72'
      },
      {
        title: '去寻找的新的主人吧！',
        url: 'http://img4.imgtn.bdimg.com/it/u=2748975304,2710656664&fm=26&gp=0.jpg'
      },
      {
        title: '去寻找的新的主人吧！',
        url: 'http://img2.imgtn.bdimg.com/it/u=1561660534,130168102&fm=26&gp=0.jpg'
      },
      {
        title: '去寻找的新的主人吧！',
        url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg'
      }
    ],


    params: {
      page: 1,
      size: 999999,
      userIdss: ''
    },

    params2: {
      friendId: '',
      trendsId: '',
      contentInfo: ''
    },


    addCartype: false, //购物车状态



    oneBookarr: [],


    // bookFriendsarr: [],

    loading1: true,


    loading2: true

    // list: [],

    // loading: true
  },

  //解析场景之
  getKey(params) {
    var _this = this
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/scene/values/auth',
      method: 'POST',
      data: params,
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.success) {
          wx.setNavigationBarTitle({
            title: res.data.data.username + '的个人详情'
          })
          var userids = "params.userIdss"
          _this.setData({
            [userids]: res.data.data.userId
          })
          _this.gettSdxTrends(_this.data.params)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  onLoad (options) {
    this.getHeight()

    if (options.scene) {
      var params = {
        sceneKey: decodeURIComponent(options.scene)
      }
      this.getKey(params)
    }else{
      wx.setNavigationBarTitle({
        title: options.name + '的个人详情'
      })
      var userids = "params.userIdss"
      this.setData({
        [userids]: options.userids
      })
      this.gettSdxTrends(this.data.params)
    }
  },
  // onShow() {
  //   this.getHeight()
  //   this.setData({
  //     token: wx.getStorageSync('token')
  //   })

  //   if (this.data.token != '' && this.data.token != null && this.data.token != undefined) {
  //     if (this.data.currentTab == 0) {
  //       this.setData({
  //         loading2: true
  //       })
  //       this.gettSdxTrends(this.data.params)
  //     } else {
  //       this.getoneBook()
  //       this.getCommons()
  //       this.setData({
  //         loading1: true
  //       })
  //       // this.gettSdxTrends(this.data.params)
  //     }
  //   } else {
  //     return false
  //   }

  // },

  //获取屏幕高度
  getHeight() {
    var _this = this
    wx.getSystemInfo({
      success: function (res) {
        let clientHetght = res.windowHeight
        let clientWidth = res.windowWidth
        let ratio = 750 / clientWidth
        let height = clientHetght * ratio - 12
        _this.setData({
          height: height
        })
      },
    })
  },
  // 点击头部切换
  clickTab(e) {
    console.log(e.currentTarget.dataset.tabindex)

    this.setData({
      currentTab: e.currentTarget.dataset.tabindex
    })

    if (this.data.currentTab == 0) {
      if (this.data.loading1 == false) {
        return false
      } else {
        this.gettSdxTrends(this.data.params)
      }
    }

    if (this.data.currentTab == 1) {
      if (this.data.loading2 == false) {
        return false
      } else {
        this.getoneBook()
        this.getCommons()
      }
    }
  },
  //切换
  switchTab(event) {
    var cur = event.detail.current
    this.setData({
      currentTab: cur
    })


    if (this.data.currentTab == 0) {
      if (this.data.loading1 == false) {
        return false
      } else {
        this.gettSdxTrends(this.data.params)
      }
    }

    if (this.data.currentTab == 1) {
      if (this.data.loading2 == false) {
        return false
      } else {
        this.getoneBook()
        this.getCommons()
      }
    }
  },

  //发布动态
  fabuDongtai() {
    wx.navigateTo({
      url: '/package/my/pages/fabuDongtai/fabuDongtai',
    })
  },

  //打开发现好友列表

  findPerson() {

    this.getoneBook()
    // wx.navigateTo({
    //   url: '/package/guangchang/pages/findPerson/findPerson',
    // })
  },

  //scroll好友滚动页面
  scrollToLower() {
    console.log(1111111)
  },
  //scroll广场滚动页面
  scrollToLower2() {
    console.log(22222222)
  },
  //查看全部评论
  lookPinglun(e) {

    var trendsid = e.currentTarget.dataset.trendsid
    wx.navigateTo({
      url: '/package/guangchang/pages/morePinglun/morePinglun?trendsid=' + trendsid,
    })
  },

  //打开个人详情
  personDetail() {
    wx.navigateTo({
      url: '/package/guangchang/pages/personDetail/personDetail',
    })
  },

  //弹出模态弹窗
  open(e) {

    var userid = e.currentTarget.dataset.id


    var bookid = e.currentTarget.dataset.bookid
    var _this = this
    wx.showActionSheet({
      itemList: ['取消关注'],
      success: function (res) {
        console.log(res)
        if (res.tapIndex == 0) {
          _this.deteleApi(bookid)
        }
      }
    })
  },

  deteleApi(bookid) {
    console.log(bookid)

    var _this = this
    var params = {
      id: bookid
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/tSdxFocus/cancelMutualBookFriend/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {

          wx.showToast({
            title: '取消关注成功',
            icon: 'none'
          })


          _this.gettSdxTrends(_this.data.params)
          // for (var i = 0; i < _this.data.bookFriendsarr.length; i++) {
          //   var guanzhu = "bookFriendsarr[" + i + "].guanzhu"
          //   _this.setData({
          //     [guanzhu]: '你没有关注他'
          //   })
          // }






        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  // onShow () {

  //   // this.setData({
  //   //   loading1: false,
  //   //   loading2: false
  //   // })

  //   // this.getoneBook()
  // },

  //查找发布动态列表
  gettSdxTrends(params) {
    var _this = this
    wx.showLoading({
      title: "加载中",
      mask: true
    })
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/SdxCommons/findTrendsAndCommons/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {

          wx.hideLoading()
          // var list = res.data.data

          for (var i = 0; i < res.data.data.length; i++) {

            res.data.data[i].trendTime = _this.formatDate(new Date(res.data.data[i].trendTime))
            res.data.data[i].contentText = ""
            if (res.data.data[i].bookInfos == '' || res.data.data[i].bookInfos == null || res.data.data[i].bookInfos == undefined) {
              res.data.data[i].pictures = res.data.data[i].pictures.split(',')
            }

            if (res.data.data[i].commonsPosList.length != 0 && res.data.data[i].commonsPosList.length > 3) {
              res.data.data[i].commonsPosList = res.data.data[i].commonsPosList.slice(0, 3)
            }
          }
          _this.setData({
            bookFriendsarr: res.data.data,
            loading1: false
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  //获取跟我看过同样书籍的人
  getoneBook() {
    var _this = this
    wx.showLoading({
      title: "广场更新中",
      mask: true
    })
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/SdxCommons/findFriendTrendsByUserId/auth',
      data: {},
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.hideLoading()
          _this.setData({
            loading2: false,
            oneBookarr: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  // 广场关注
  addGuanzhu(e) {
    console.log(e)

    var userids = e.currentTarget.dataset.userid

    var username = e.currentTarget.dataset.username

    this.addFriend(userids, username)
  },


  addFriend(userids, username) {
    var _this = this
    var params = {
      bookFriendId: userids,
      bookFriendName: username
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/tSdxFocus/mod/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '关注成功',
            icon: 'none',
            duration: 2000
          })

          _this.gettSdxTrends(_this.data.params)




          // var arr = _this.data.oneBookarr
          // for (var i = 0; i < _this.data.oneBookarr.length; i++) {
          //   if (userids == _this.data.oneBookarr[i].userId) {
          //     var guanzhu = "oneBookarr[" + i + "].guanzhu"
          //     _this.setData({
          //       [guanzhu]: '你已经关注了他'
          //     })
          //   }
          // }

          setTimeout(function () {
            _this.setData({
              currentTab: 0
            })
            _this.gettSdxTrends(_this.data.params)
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  addCar(e) {
    var carid = e.currentTarget.dataset.carid

    this.addCarApi(carid)
  },

  //添加购物车

  addCarApi(id) {

    var _this = this

    var params = {
      userId: wx.getStorageSync('userId'),
      bookInfoId: id
    }

    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/sdxShoppingTrolleys/mod/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        console.log(res)

        if (res.data.code == 200) {
          wx.showToast({
            title: '购物车添加成功',
            icon: 'none'
          })
          _this.setData({
            addCartype: true
          })
          setTimeout(function () {
            _this.setData({
              addCartype: false
            })
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  //跳转书本详情
  //书本详情
  bookdetail(e) {
    console.log(e)
    var bookId = e.currentTarget.dataset.bookid
    wx.navigateTo({
      url: '/package/index/pages/bookdetail/bookdetail?bookId=' + bookId,
    })
  },

  //动态点赞操作
  dianzanBtn(e) {
    var trendsId = e.currentTarget.dataset.trendsid
    this.dianzanBtnApi(trendsId)
  },

  dianzanBtnApi(trendsId) {

    var _this = this
    var params = {
      trendsId: trendsId
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/tSdxThu/mod/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '点赞成功',
            icon: 'none'
          })
          for (var i = 0; i < _this.data.bookFriendsarr.length; i++) {
            if (trendsId == _this.data.bookFriendsarr[i].trendsId) {

              // debugger

              var type = "bookFriendsarr[" + i + "].type"
              var zanNum = "bookFriendsarr[" + i + "].zanNum"

              var TSdxThuPoList = "bookFriendsarr[" + i + "].TSdxThuPoList"

              var arr = _this.data.bookFriendsarr[i].TSdxThuPoList
              arr.unshift({

                "zanPic": wx.getStorageSync('avatarUrl')

              })

              // var arr = [{
              //     "zanPic": wx.getStorageSync('avatarUrl')
              // }]

              _this.setData({
                [type]: 0,
                [zanNum]: _this.data.bookFriendsarr[i].zanNum += 1,
                [TSdxThuPoList]: arr.length > 3 ? arr.splice(0, 3) : arr
              })
            }
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  delZan(e) {

    var _this = this
    var trendsId = e.currentTarget.dataset.trendsid
    this.delZanApi(trendsId)
  },

  delZanApi(trendsId) {

    var _this = this

    var params = {
      trendsId: trendsId
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/tSdxThu/del/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '取消点赞成功',
            icon: 'none'
          })
          for (var i = 0; i < _this.data.bookFriendsarr.length; i++) {
            if (trendsId == _this.data.bookFriendsarr[i].trendsId) {

              var type = "bookFriendsarr[" + i + "].type"
              var zanNum = "bookFriendsarr[" + i + "].zanNum"

              for (var j = 0; j < _this.data.bookFriendsarr[i].TSdxThuPoList.length; j++) {
                if (wx.getStorageSync("avatarUrl") == _this.data.bookFriendsarr[i].TSdxThuPoList[j].zanPic) {

                  var TSdxThuPoList = "bookFriendsarr[" + i + "].TSdxThuPoList"

                  var arr = _this.data.bookFriendsarr[i].TSdxThuPoList

                  arr.splice(j, 1)

                  _this.setData({
                    [TSdxThuPoList]: arr
                  })

                }
              }

              _this.setData({
                [type]: 1,
                [zanNum]: _this.data.bookFriendsarr[i].zanNum -= 1
              })
            }
          }
        }
      }
    })
  },


  // //添加评论

  // bindcontentInfo (e) {

  //   var contentInfo = e.detail.value

  //   var info = "params2.contentInfo"

  //   this.setData({
  //     [info]: contentInfo
  //   })
  // },

  addPinglunApi(e) {
    var id = e.currentTarget.dataset.friendid

    var trendsid = e.currentTarget.dataset.trendsid


    this.addPinglunApiUrl(id, trendsid)
  },


  addPinglunApiUrl(id, trendsid) {

    var _this = this
    for (var i = 0; i < this.data.bookFriendsarr.length; i++) {
      if (trendsid == this.data.bookFriendsarr[i].trendsId) {
        if (this.data.bookFriendsarr[i].contentText == '') {
          wx.showToast({
            title: '请输入评论内容',
            icon: 'none'
          })
          return
        }

        var contentInfo = "params2.contentInfo"

        _this.setData({
          [contentInfo]: _this.data.bookFriendsarr[i].contentText
        })
      }
    }

    var params = {
      friendId: id,
      trendsId: trendsid,
      contentInfo: this.data.params2.contentInfo
    }

    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/tSdxCommons/mod/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '评论成功',
            icon: 'none'
          })
          for (var i = 0; i < _this.data.bookFriendsarr.length; i++) {
            if (trendsid == _this.data.bookFriendsarr[i].trendsId) {

              var commonsPosList = "bookFriendsarr[" + i + "].commonsPosList"

              var commonsSize = "bookFriendsarr[" + i + "].commonsSize"

              var arr = _this.data.bookFriendsarr[i].commonsPosList

              var contentInfo = "bookFriendsarr[" + i + "].contentText"

              arr.unshift({
                "friendName": wx.getStorageSync('nickName'),
                "contentInfo": params.contentInfo
              })

              _this.setData({
                [commonsPosList]: arr.length > 3 ? arr.splice(0, 3) : arr,
                [commonsSize]: _this.data.bookFriendsarr[i].commonsSize += 1,
                [contentInfo]: ""
              })
            }
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  addcontentText(e) {

    var text = e.detail.value

    var index = e.currentTarget.dataset.index

    var contentText = "bookFriendsarr[" + index + "].contentText"


    this.setData({
      [contentText]: text
    })

    //  console.log(e)
  },


  //获取广场列表
  getCommons() {
    var _this = this
    wx.showLoading({
      title: "加载中",
      mask: true
    })
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/SdxCommons/findTrends/auth',
      data: {},
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {

          wx.hideLoading()
          // var list = _this.data.data

          for (var i = 0; i < res.data.data.length; i++) {

            res.data.data[i].bookPictures = res.data.data[i].bookPictures.split(',')

          }
          _this.setData({
            note: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  //
  addGuanzhu2() {
    wx.showToast({
      title: '您已关注,请在关注书朋友列表进行取消关注',
      icon: 'none'
    })
    return
  },


  goDetail(e) {
    var bookid = e.currentTarget.dataset.bookinfoid

    // var bookId = e.currentTarget.dataset.bookid
    wx.navigateTo({
      url: '/package/index/pages/bookdetail/bookdetail?bookId=' + bookid,
    })
  },


  addGuanzhufriend (e) {
    var userid = e.currentTarget.dataset.user

    var name  = e.currentTarget.dataset.name

    this.addFriend(userid,name)

  },



  addGuanzhufriend2 () {

      wx.showToast({
        title: '你已经关注了他,请在书友列表进行取消',
        icon: 'none'
      })

      return
  },


  addFriend(userids, username) {
    var _this = this
    var params = {
      bookFriendId: userids,
      bookFriendName: username
    }
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/tSdxFocus/mod/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '关注成功',
            icon: 'none',
            duration: 2000
          })

          // var arr = _this.data.bookFriendsarr
          _this.gettSdxTrends(_this.data.params)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  formatDate(now) {
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var date = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    return month + "-" + date
  }
})
